#! /bin/sh

# arg1 = file
ass2srt()
{
	# http://matroska.org/technical/specs/subtitles/ssa.html
	
	FILE_IN="$1"
	FILE_OUT="$(echo "$1" | sed -e 's|.ass|.srt|g')"
	cat "$FILE_IN" | grep 'Dialogue' > "$FILE_OUT"
	avconv -i "$FILE_IN" -c:s srt -y "$FILE_OUT"
#	rm "$FILE_IN"
}

# arg1 = file
# arg2 = track id
extract()
{
	#mkvinfo "$@" | grep --before-context=2 --after-context=11 'Track type: subtitle' | grep 'Track number:'
	#mkvinfo "$@" | grep --before-context=2 --after-context=11 'Track type: subtitle' | grep 'Language:'
	#mkvinfo "$@" | grep --before-context=2 --after-context=11 'Track type: subtitle' | grep 'Codec ID:'
	TRACK="$2"
	FILE_IN="$1"
	FILE_OUT="$(echo "$1" | sed -e 's|.mkv|.ass|g')"
	mkvextract tracks "$FILE_IN" "$TRACK":"$FILE_OUT"
	ass2srt "$FILE_OUT"
}



#find /home/ronny/Desktop/vidtest -type f

extract '/home/ronny/Desktop/vidtest/Katanagatari Promotion 01.mkv' 3
